use std::sync::{Arc, Mutex};
use std::thread;
use crossbeam_channel::{Receiver, Sender};
use crate::Number;

#[derive(Clone)]
pub(crate) struct PoolMetrics {
    pub name:String,
    pub max_active_requests:Arc<Mutex<Number>>,
    pub executed:Arc<Mutex<Number>>,
    pub sender:Sender<PoolMetricsUpdate>,
}


impl PoolMetrics {
    pub fn new(name:String) -> Self {
        let (a,b) = crossbeam_channel::bounded(10);
        let mut m = PoolMetrics{
            name,
            max_active_requests: Arc::new(Mutex::new(Number::new())),
            executed:Arc::new(Mutex::new(Number::new())),
            sender: a
        };

        let exec = m.executed.clone();
        let requests = m.max_active_requests.clone();

        thread::spawn(move || {
            for i in b.iter() {
                exec.lock().unwrap().increment(1f64);
                requests.lock().unwrap().update_max(i.active_count as f64);
            }
        });

        m
    }
}

impl PoolMetrics {
    pub fn reset(&mut self) {
        self.max_active_requests = Arc::new(Mutex::new(Number::new()));
        self.executed = Arc::new(Mutex::new(Number::new()));
    }
}


pub struct PoolMetricsUpdate {
    active_count:i64,
}

#[cfg(test)]
mod test {
    #[test]
    fn test () {

    }
}